package galaxy;


import irio.gamer.gamer;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;

import Logic.GalaxyComponent;
import Logic.GamerParams;

import utils.JDouble;
import utils.JMyCombo;
import utils.Vec;

public class ComponentDetailPanel extends JPanel{

	public GalaxyComponent current = null;
	
	public ComponentDetailPanel() {
		//Set(gamer.galaxy.components.get(0));
	}
	JMyCombo cmbType = null;

	public void clear() {
		removeAll();
		revalidate();  
		repaint();

	}
	
	public void Set(GalaxyComponent gc) {
		current = gc;
		removeAll();

		
		setLayout(new GridLayout(13,1));
		
		cmbType = new JMyCombo("Component type", current.type, GalaxyComponent.types, GalaxyComponent.typesValues);
		
        cmbType.combo.addActionListener(new ActionListener()
        {
//            @Override
            public void actionPerformed(ActionEvent event)
            {
               //When the fruit of veg button is pressed
               //the setVisible value of the listPanel and
               //comboPanel is switched from true to 
               //value or vice versa.
            	cmbType.vec.x = cmbType.values[cmbType.combo.getSelectedIndex()];
            	GalaxyComponentPanel.cpl.reload();
				gamer.imagePanel.preview.resetPreview(false);

            	
            }
        });

		
		add(cmbType);
		add(new JMyCombo("Spectrum", current.spectrum, gamer.spectra.spectra, gamer.spectra.spectraVal));
		add(new JDouble("Intensity", current.intensity_modifier, new Vec(0,0,0), new Vec(100000,0,0), GamerParams.decimal));
	//	add(new JDouble("Spectrum", current.spectrum, new Vec(0,0,0), new Vec(3,0,0), GamerParams.integer));

		if (current.type.x == 0) {// bulge
			add(new JDouble("Size", current.armstrength, new Vec(0,0,0), new Vec(70,0,0), GamerParams.decimal));
			add(new JDouble("Height (z0)", current.z0, new Vec(0,0,0), new Vec(10,0,0), GamerParams.decimal));
			add(new JDouble("Dropoff (r0)", current.r0, new Vec(0,0,0), new Vec(10,0,0), GamerParams.decimal));
		}
		else 
		{
			add(new JDouble("Arm strength", current.armstrength, new Vec(0,0,0), new Vec(70,0,0), GamerParams.decimal));
			add(new JDouble("Height (z0)", current.z0, new Vec(0,0,0), new Vec(1,0,0), GamerParams.decimal));
			add(new JDouble("Dropoff (r0)", current.r0, new Vec(0,0,0), new Vec(3,0,0), GamerParams.decimal));
			add(new JDouble("Twirl", current.winding, new Vec(0,0,0), new Vec(2.0,0,0), GamerParams.decimal));
			add(new JDouble("Distortion", current.delta, new Vec(-4,0,0), new Vec(4.0,0,0), GamerParams.decimal));
			add(new JDouble("Noise scale", current.scale, new Vec(0,0,0), new Vec(100,0,0), GamerParams.decimal));
			add(new JDouble("Noise offset", current.noiseOffset, new Vec(-20,0,0), new Vec(20,0,0), GamerParams.decimal));
			add(new JDouble("Noise tilt", current.noiseTilt, new Vec(0,0,0), new Vec(15,0,0), GamerParams.decimal));
		}
		add(new JMyCombo("Active", current.active, GamerParams.yesno, GamerParams.yesnoVals));

		JPanel upDown = new JPanel();
		upDown.setLayout(new GridLayout(1,2));
		JButton up = new JButton("up");
		JButton down = new JButton("down");
		upDown.add(up);
		upDown.add(down);
		add(upDown);
		
		up.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int idx = -1;
				for (int i=0;i<gamer.galaxy.components.size();i++)
					if (gamer.galaxy.components.get(i) == current)
						idx = i;
				
				if (idx == 0)
					return;
				gamer.galaxy.components.remove(idx);
				gamer.galaxy.components.add(idx-1, current);
				for (int i=0;i<gamer.galaxy.components.size();i++)
					gamer.galaxy.components.get(i).component_no = i+1;
				GalaxyComponentPanel.cpl.reload();
				gamer.imagePanel.preview.resetPreview(false);
			}
		});

		down.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int idx = -1;
				for (int i=0;i<gamer.galaxy.components.size();i++)
					if (gamer.galaxy.components.get(i) == current)
						idx = i;
				
				if (idx == gamer.galaxy.components.size()-1)
					return;
				gamer.galaxy.components.remove(idx);
				gamer.galaxy.components.add(idx+1, current);
				for (int i=0;i<gamer.galaxy.components.size();i++)
					gamer.galaxy.components.get(i).component_no = i+1;
				GalaxyComponentPanel.cpl.reload();
				gamer.imagePanel.preview.resetPreview(false);
			}
		});

		
		revalidate();  
		repaint();
	}
	
	
}
